﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:张谦
// ===============================================================================

using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Windows;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统用户服务
    /// </summary>
    public class SysUserService
    {
        private const string RootUrl = "/api/sys/user";
        public static SysUserService Instance { get; } = new SysUserService();

        /// <summary>
        /// 新建用户模型
        /// </summary>
        public async Task<SysUser> NewAsync()
        {
            return await Task.FromResult(new SysUser
            {
                IsAdmin = false,
                LoginTimes = 0,
                Category = 0,
                Status = true,
                PwdNextLoginMustModify = false,
                PwdNeverExpire = false,
                PwdAllowModify = true
            });
        }

        /// <summary>
        /// 复制用户模型
        /// </summary>
        public async Task CopyAsync(string id)
        {
            var model = await SelectByIdAsync(id);
            model.Id = null;
            model.Name += " 复制";
            model.Account += " 复制";
            model.Pwd = model.ConfirmPwd = "xci@123";
            await SaveAsync(true,model);
        }

        /// <summary>
        /// 保存用户对象
        /// </summary>
        /// <param name="created">是否新增</param>
        /// <param name="model">应用对象</param>
        /// <returns>成功返回true</returns>
        public async Task<BoolResult> SaveAsync(bool created, SysUser model)
        {
            model.PwdAllowModify = !model.PwdDenyModify;
            return await App.Client.PostAsBoolResultAsync($"{RootUrl}/{(created ? "insert" : "update")}", model);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        /// <returns>成功返回true</returns>
        public async Task UpdateStatusAsync(string ids, bool status)
        {
            await App.Client.PostAsync($"{RootUrl}/updateStatus", new { ids, status });
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 根据主键获取用户对象
        /// </summary>
        /// <param name="id">用户主键</param>
        public async Task<SysUser> SelectByIdAsync(string id)
        {
            var model = await App.Client.PostAsync<SysUser>($"{RootUrl}/selectById", new { id });
            model.PwdDenyModify = !model.PwdAllowModify;
            return model;
        }

        /// <summary>
        /// 根据主键获取用户对象
        /// </summary>
        /// <param name="id">用户主键</param>
        public async Task<SysUser> SelectEditUserByIdAsync(string id)
        {
            var model = await App.Client.PostAsync<SysUser>($"{RootUrl}/selectEditUserById", new { id });
            model.PwdDenyModify = !model.PwdAllowModify;
            return model;
        }

        /// <summary>
        /// 根据主键获取用户对象
        /// </summary>
        /// <param name="args">编辑参数</param>
        public Task<SysUser> GetOrCreateAsync(EditFormArgs args)
        {
            if (args == null || args.Created) return NewAsync();
            return SelectEditUserByIdAsync(args.Id);
        }

        /// <summary>
        /// 根据用户账号获取用户对象
        /// </summary>
        /// <param name="account">用户账号</param>
        /// <returns>成功返回模型对象</returns>
        public async Task<SysUser> SelectByAccountAsync(string account)
        {
            var model = await App.Client.PostAsync<SysUser>($"{RootUrl}/selectByAccount", new { account });
            model.PwdDenyModify = !model.PwdAllowModify;
            return model;
        }

        /// <summary>
        /// 查询用户分页列表
        /// </summary>
        /// <param name="filter">过滤器</param>
        public async Task<PagingList<SysUser>> SelectPageListAsync(SysUserFilter filter)
        {
            return await App.Client.PostAsync<PagingList<SysUser>>($"{RootUrl}/selectPageList", filter);
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="filter">过滤器</param>
        public async Task<List<SysUser>> SelectListAsync(SysUserFilter filter)
        {
            return await App.Client.PostAsync<List<SysUser>>($"{RootUrl}/selectList", filter);
        }

        /// <summary>
        /// 导出用户列表
        /// </summary>
        /// <param name="filePath">保存路径</param>
        /// <param name="filter">过滤器</param>
        public async Task ExportAsync(string filePath, SysUserFilter filter)
        {
            await App.Client.DownloadAsync($"{RootUrl}/export", filePath, filter);
        }

        /// <summary>
        /// 刷新服务端用户缓存
        /// </summary>
        public async Task RefreshCacheAsync()
        {
            await App.Client.PostAsync($"{RootUrl}/refreshCache");
        }
    }
}
